package nsn.message.aggregator.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 
 * @author Lukasz Nowak
 * @since 10-03-2011
 * 
 */
public class Source implements ISource {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3151946724602019185L;
	private String _name;
	private List<IEntry> _entries = new ArrayList<IEntry>();
	private AbstractSourceType _type;
	private final transient PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
			this);

	@Override
	public String getName() {
		return _name;
	}

	/**
	 * @param name
	 * 
	 */
	public void setName(final String name) {
		_name = name;
	}

	/**
	 */
	@Override
	public List<IEntry> getEntries() {
		return _entries;
	}

	/**
	 */
	@Override
	public ISourceType getType() {
		return _type;
	}

	@Override
	public boolean equals(final Object obj) {
		if (obj instanceof Source) {
			Source source = (Source) obj;
			return _name.equals(source._name) && _type.equals(source._type);
		}
		return false;
	}

	@Override
	public int hashCode() {
		return _name.hashCode();
	}


	/**
	 * 
	 * 
	 * @param listener
	 * @see nsn.message.aggregator.model.behaviour.Observable#addPropertyChangeListener(java.beans.PropertyChangeListener)
	 */
	@Override
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		propertyChangeSupport.addPropertyChangeListener(listener);

	}

	/**
	 * 
	 * 
	 * @param listener
	 * @see nsn.message.aggregator.model.behaviour.Observable#removePropertyChangeListener(java.beans.PropertyChangeListener)
	 */
	@Override
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		propertyChangeSupport.removePropertyChangeListener(listener);

	}

	/**
	 *
	 * @see nsn.message.aggregator.model.behaviour.Deletable#delete()
	 */
	@Override
	public void delete() {
		_type.delete(this);
	}

	/**
	 *
	 * @see nsn.message.aggregator.model.behaviour.Updatable#update()
	 */
	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @param type
	 * 
	 * 
	 */
	public void setType(final AbstractSourceType type) {
		_type = type;
	}



	/**
	 * 
	 * 
	 * @param entry
	 */
	public void delete(IEntry entry) {
		getEntries().remove(entry);
		propertyChangeSupport.firePropertyChange("EntryDeleted", null, entry);
	}

}
